<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :data="(header_list as any)"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="c_666 fw flex">
          <div
            class="top_item"
            @click="to_project_list()"
            @mouseenter="svg_color_list[0] = '#3580eb'"
            @mouseleave="svg_color_list[0] = '#666'"
          >
            <svgBtn
              icon="to"
              style="font-size: 12px !important; transform: rotate(180deg)"
              class="flex_cc m_right5"
              :color="svg_color_list[0]"
            ></svgBtn>
            项目列表
          </div>
          <div class="line"></div>
          <div>工程列表</div>
        </div>
        <div class="flex">
          <!-- <el-button type="primary" @click="show_box = true">新增</el-button> -->
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        v-loading="loading"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 4 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="名称"
          prop="name"
          key="名称"
          sortable
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="代号"
          prop="code"
          key="代号"
          sortable
          v-if="header_list[1].showTable"
        ></el-table-column>
        <el-table-column
          label="状态"
          key="状态"
          sortable
          prop="status"
          v-if="header_list[2].showTable"
          ><template #default="scope: any">
            <div v-if="scope.row.status == 1">待发布</div>
            <div v-if="scope.row.status == 2">已发布</div>
            <div v-if="scope.row.status == 4">回收站</div></template
          ></el-table-column
        >
        <el-table-column label="操作" width="700px">
          <template #default="scope: any">
            <el-button @click="refresh_list(scope)">刷新</el-button>
            <!-- <el-button @click="edit_list(scope)">编辑</el-button> -->
            <!-- <el-button v-if="scope.row.status != 4" @click="delete_list(scope)"
              >删除</el-button
            >
            <el-button v-else @click="delete_list(scope)">还原</el-button> -->
            <!-- <el-button type="primary" @click="show_metadata_box(scope)"
              >元数据管理</el-button
            > -->
            <el-button type="primary" @click="to_process(scope)"
              >工序管理</el-button
            >
            <!-- <el-button type="primary" @click="to_view(scope)"
              >视图管理</el-button
            > -->
            <!-- <el-button
              type="primary"
              v-if="scope.row.status == 1"
              @click="to_publish(scope)"
              >发布</el-button
            >
            <el-button
              type="primary"
              v-if="scope.row.status == 2"
              @click="to_publish(scope)"
              >撤销</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <div>
        <div style="margin-top: 30px"></div>
      </div>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
  <el-drawer
    v-model="show_box"
    direction="rtl"
    size="60%"
    destroy-on-close
    @close="resetForm(addEngineerform)"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4 v-if="add_form_data.id == ''">新增工程</h4>
      <h4 v-else>编辑工程</h4>
    </template>
    <template #default>
      <el-form
        :model="add_form_data"
        ref="addEngineerform"
        :rules="engineer_rules"
        label-width="120px"
      >
        <el-form-item label="类型">
          <el-select v-model="add_form_data.type">
            <el-option
              v-for="item in szls_engineer_tyle_list"
              :key="item.dictValue"
              :value="item.dictCode"
              :label="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否关联">
          <el-radio v-model="add_form_data.isCollection" :label="0"
            >否</el-radio
          >
          <el-radio v-model="add_form_data.isCollection" :label="1"
            >是</el-radio
          >
        </el-form-item>
        <el-form-item label="关联工程" v-if="add_form_data.isCollection">
          <el-select v-model="add_form_data.engineeringIdList" multiple>
            <el-option
              v-for="item in list"
              :value="item.id"
              :key="item.id"
              :label="item.name"
              :disabled="item.id == add_form_data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="add_form_data.name"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="代号" prop="code">
          <el-input
            v-model="add_form_data.code"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="数据推送频率(秒)">
          <el-input
            type="number"
            :maxlength="400"
            v-model="add_form_data.pushFrequency"
            placeholder="请输入(5以及5的倍数)"
          ></el-input>
        </el-form-item>
        <el-form-item label="工程附件（zip,rar,7z）" prop="attachment">
          <upload
            :imageUrl="add_form_data.attachment"
            :width="100"
            :height="100"
            :accept="'zip,rar,7z'"
          ></upload>
        </el-form-item>
        <el-form-item label="工程预览图">
          <upload
            :imageUrl="add_form_data.pic"
            :width="240"
            :height="160"
            :accept="accept"
          ></upload>
        </el-form-item>

        <div class="flex" v-if="add_form_data.status == '2'">
          <el-form-item label="发布时间">
            <div>
              {{
                formatDate(parseInt(add_form_data.publishTime), 'yyyy-MM-dd')
              }}
            </div>
          </el-form-item>
          <el-form-item label="发布人">
            <div>{{ add_form_data.publishByName }}</div>
          </el-form-item>
        </div>
        <div class="m_top10 tr">
          <el-button @click="resetForm(addEngineerform)">取消</el-button>
          <el-button
            type="primary"
            :loading="btn_loading"
            @click="submit_engineer(addEngineerform)"
            >确定</el-button
          >
        </div>
      </el-form>
    </template>
  </el-drawer>
  <el-drawer
    v-model="metadata_box"
    direction="rtl"
    size="80%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <template #title><h4>元数据列表</h4></template>
    <template #default>
      <div class="flex_js">
        <div class="flex">
          <el-input
            v-model="metadata_page_data.name"
            placeholder="请输入"
            clearable
          ></el-input>
          <el-button
            class="m_left10"
            type="primary"
            @click=";(metadata_page_data.current = 1), get_metadata_list()"
            >搜索</el-button
          >
        </div>
        <div class="flex">
          <el-upload
            class="avatar-uploader m_right10"
            accept="#"
            :action="uploadUrl"
            :data="fileUpData"
            :show-file-list="false"
            :on-progress="
              () => {
                btn_loading = true
              }
            "
            :on-success="handleUploadSuccess"
            :headers="header"
            auto-upload
          >
            <el-button type="primary" :loading="btn_loading">上传</el-button>
          </el-upload>
          <el-button type="primary" @click="add_metadata_box = true"
            >新增</el-button
          >
        </div>
      </div>

      <el-table
        style="text-align: center; margin-top: 10px"
        :data="metadata_list"
        :max-height="metadata_max_height"
        border
        v-loading="loading"
        :default-sort="{ prop: 'sort' }"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 4 ? '0' : '',
        }"
      >
        <el-table-column label="名称" prop="name" sortable></el-table-column>
        <el-table-column label="编号" prop="code" sortable></el-table-column>
        <el-table-column label="排序" prop="sort" sortable></el-table-column>

        <el-table-column label="类型" sortable prop="type">
          <template #default="scope: any">
            <div v-if="scope.row.type == 1">文本</div>
            <div v-if="scope.row.type == 2">数字</div>
            <div v-if="scope.row.type == 6">判断</div>
          </template> </el-table-column
        ><el-table-column label="组合类型" sortable prop="isCombination">
          <template #default="scope: any">
            <div v-if="scope.row.isCombination" style="color: #3580eb">是</div>
            <div v-else>否</div>
          </template> </el-table-column
        ><el-table-column label="状态" sortable prop="status"
          ><template #default="scope: any">
            <div v-if="!scope.row.status">停用</div>
            <div v-else style="color: #3580eb">启用</div></template
          ></el-table-column
        >
        <el-table-column label="备注" prop="remark" sortable></el-table-column>
        <el-table-column label="操作">
          <template #default="scope: any">
            <el-button @click="edit_metadata_list(scope)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="metadata_page_data.current"
        v-model:page-size="metadata_page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="metadata_total"
        @size-change="metadata_list_debounce()"
        @current-change="metadata_list_debounce()"
      />
    </template>
  </el-drawer>
  <el-drawer
    v-model="add_metadata_box"
    direction="rtl"
    size="50%"
    destroy-on-close
    @close="resetForm(addmetadataform)"
    :close-on-click-modal="false"
  >
    <template #title
      ><h4 v-if="add_metadata_form_data.id == ''">新增</h4>
      <h4 v-else>编辑</h4>
    </template>
    <template #default>
      <el-form
        :model="add_metadata_form_data"
        :rules="metadata_rules"
        label-width="120px"
        ref="addmetadataform"
      >
        <el-form-item label="组合类型">
          <el-radio
            v-model="add_metadata_form_data.isCombination"
            :label="false"
            >否</el-radio
          >
          <el-radio v-model="add_metadata_form_data.isCombination" :label="true"
            >是</el-radio
          >
        </el-form-item>
        <el-form-item
          label="组合所需参数"
          v-if="add_metadata_form_data.isCombination"
          prop="paraList"
        >
          <el-select
            style="width: 100%"
            v-model="add_metadata_form_data.paraList"
            filterable
            multiple
          >
            <el-option
              v-for="item in select_metadata_list"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="组合取值公式"
          v-if="add_metadata_form_data.isCombination"
          prop="value"
        >
          <el-input
            v-model="add_metadata_form_data.value"
            placeholder="请输入"
            type="textarea"
            show-word-limit
            maxlength="10000"
          ></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="add_metadata_form_data.name"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input> </el-form-item
        ><el-form-item label="代号" prop="code">
          <el-input
            v-model="add_metadata_form_data.code"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input> </el-form-item
        ><el-form-item label="排序" prop="sort">
          <el-input
            v-model="add_metadata_form_data.sort"
            placeholder="请输入"
            show-word-limit
            maxlength="4"
          ></el-input> </el-form-item
        ><el-form-item label="类型" prop="type">
          <el-select v-model="add_metadata_form_data.type">
            <el-option value="1" label="文本"></el-option>
            <el-option value="2" label="数字"></el-option>
            <el-option value="6" label="判断"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-model="add_metadata_form_data.status" :label="true"
            >启用</el-radio
          >
          <el-radio v-model="add_metadata_form_data.status" :label="false"
            >停用</el-radio
          >
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            v-model="add_metadata_form_data.remark"
            placeholder="请输入"
            show-word-limit
            maxlength="300"
          ></el-input>
        </el-form-item>
        <div class="m_top10 tr">
          <el-button @click="resetForm(addmetadataform)">取消</el-button>
          <el-button
            type="primary"
            :loading="btn_loading"
            @click="submit_metadata(addmetadataform)"
            >确定</el-button
          >
        </div>
      </el-form>
    </template>
  </el-drawer>
  <viewSetup
    :show_viewSetup="show_viewSetup"
    @close_show_viewSetup="show_viewSetup = false"
    :engineeringId="engineeringId"
    :projectId="props.projectId"
  ></viewSetup>
</template>

<script lang="ts" setup>
import { dictData, debounce, formatDate } from '@/utils/utils' // 权限校验
import viewSetup from './viewSetup.vue'
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, nextTick, computed } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import upload from '@/view/components/upload.vue'
import svgBtn from '@/components/svg_button.vue'
import { getCookie, setCookie } from '@/utils/cookie'
import { getBaseURL } from '@/service/BaseURL'
import { FormInstance, ElMessage, ElMessageBox } from 'element-plus'
import store from '@/store'
// type FormInstance = InstanceType<typeof ElForm>
const addEngineerform = ref<FormInstance>()
const addmetadataform = ref<FormInstance>()

const accept = 'image/jpeg,image/jpg,image/png'
const attachmentUpload = (rule, value, callback) => {
  if (!add_form_data.value.attachment.length) {
    callback(new Error('请上传工程附件'))
  } else {
    callback()
  }
}
const engineer_rules = ref({
  // 表单验证规则
  name: [{ required: true, message: '请输入工程名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入工程代号', trigger: 'blur' }],
  attachment: [
    {
      validator: attachmentUpload,
      required: true,
    },
  ],
})
const metadata_rules = ref({
  name: [{ required: true, message: '请输入元数据名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入元数据代号', trigger: 'blur' }],
  sort: [{ required: true, message: '请输入元数据排序', trigger: 'blur' }],
  type: [{ required: true, message: '请输入元数据类型', trigger: 'change' }],
  value: [{ required: true, message: '请输入组合取值公式', trigger: 'blur' }],
  paraList: [
    { required: true, message: '请选择组合所需参数', trigger: 'change' },
  ],
})
const svg_color_list = ref(['#666'])
const list = ref()
const total = ref()
const max_height = ref()
const metadata_max_height = ref()
nextTick(() => {
  max_height.value = window.innerHeight - 320 // 带分页的 table 高度
  metadata_max_height.value = window.innerHeight - 170
})
const loading = ref(false)
const show_box = ref(false)
const height_size = ref(1)
const header_list = ref([
  {
    label: '名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '代号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: 1,
        label: '待发布',
      },
      {
        value: 2,
        label: '已发布',
      },
      {
        value: 4,
        label: '回收站',
      },
    ],
  },
])
const props = defineProps({
  projectId: {
    type: String,
  },
})
const page_data = ref({
  current: 1,
  pageSize: 10,
  name: '',
  code: '',
  projectId: computed(() => {
    return props.projectId
  }),
  status: '',
})
const add_form_data = ref({
  id: '',
  name: '',
  code: '',
  pic: [],
  attachment: [], // 工程附件
  type: '1',
  isCollection: 0,
  status: '1',
  engineeringIdList: [],
  publishTime: '',
  publishByName: '',
  pushFrequency: '', // 数据推送频率
  projectId: computed(() => {
    return props.projectId
  }),
})

const add_metadata_form_data = ref({
  id: '',
  name: '',
  code: '',
  sort: '',
  remark: '',
  status: true,
  type: '',
  engineeringId: '',
  isCombination: false,
  value: '',
  paraList: [],
  projectId: computed(() => {
    return props.projectId
  }),
})
const szls_engineer_tyle_list = ref() //  数字孪生 工程类型
onMounted(() => {
  console.log(123)
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('操作成功！')
  })
  setTimeout(() => {
    get_list()
  }, 100)
  dictData('1564425513282310146').then((res) => {
    szls_engineer_tyle_list.value = res as any
  }) // 获取字典值 数字孪生 工程类型
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const show_viewSetup = ref(false)
const to_view = (e) => {
  engineeringId.value = e.row.id
  EventBus.emit('show_viewSetup', {
    engineeringId: e.row.id,
    projectId: props.projectId,
  })
  show_viewSetup.value = true
}
const to_project_list = () => {
  EventBus.emit('list_change', 1)
}
const to_process = (e) => {
  EventBus.emit('list_change', 3)
  setTimeout(() => {
    EventBus.emit('engineeringId', e.row.id)
  })
}
const edit_metadata_list = (e) => {
  add_metadata_form_data.value = {
    id: e.row.id,
    name: e.row.name,
    code: e.row.code,
    sort: e.row.sort,
    remark: e.row.remark,
    status: e.row.status,
    type: e.row.type + '',
    isCombination: e.row.isCombination,
    paraList: [],
    value: e.row.value,
    engineeringId: add_metadata_form_data.value.engineeringId,
    projectId: add_metadata_form_data.value.projectId,
  }
  if (e.row.isCombination) {
    e.row.paraList.forEach((item) => {
      add_metadata_form_data.value.paraList.push(item.metadataId)
    })
  }
  add_metadata_box.value = true
}
const submit_metadata = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      if (add_metadata_form_data.value.isCombination) {
        let list = []
        add_metadata_form_data.value.paraList.forEach((item) => {
          let obj = { metadataId: '' }
          obj.metadataId = item
          list.push(obj)
        })
        add_metadata_form_data.value.paraList = list
      } else {
        add_metadata_form_data.value.paraList = []
      }
      axios
        .request({
          url:
            add_metadata_form_data.value.id == ''
              ? serviceApi.szls_engineeringMetadata_crud
              : serviceApi.szls_engineeringMetadata_crud +
                '/' +
                add_metadata_form_data.value.id,
          method: add_metadata_form_data.value.id == '' ? 'post' : 'put',
          data: add_metadata_form_data.value,
        })
        .then((res) => {
          console.log(res)
          ElMessage.success('操作成功！')
          add_metadata_box.value = false
          get_metadata_list()
          get_select_metadata_list()
        })
        .catch((err) => {
          ElMessage.error(err)
        })
    }
  })
}
const metadata_box = ref()
const add_metadata_box = ref()
const metadata_list = ref()
const metadata_total = ref()
const metadata_page_data = ref({
  current: 1,
  pageSize: 10,
  name: '',
  engineeringId: '',
  projectId: props.projectId,
})
const engineeringId = ref()
const fileUpData = ref({
  engineeringId: '',
})
const uploadUrl = getBaseURL() + serviceApi.metadata_importExcel
const header = { Authorization: getCookie('bimai_admin_token') }
const handleUploadSuccess = (res) => {
  console.log(res)
  if (res.code == 200) {
    ElMessage.success('上传成功！')
    get_metadata_list()
  } else {
    ElMessage.error(res.message)
  }
  btn_loading.value = false
}
const show_metadata_box = (e) => {
  add_metadata_form_data.value.engineeringId = e.row.id
  metadata_page_data.value.engineeringId = e.row.id
  fileUpData.value.engineeringId = e.row.id
  get_metadata_list()
  get_select_metadata_list()
  metadata_box.value = true
}
const select_metadata_list = ref()
const get_select_metadata_list = () => {
  axios
    .request({
      url: serviceApi.szls_engineeringMetadata_baseList,
      method: 'post',
      data: { engineeringId: add_metadata_form_data.value.engineeringId },
    })
    .then((res) => {
      console.log(res)
      select_metadata_list.value = res.data
    })
    .catch((err) => {
      ElMessage.error(err)
    })
}
const get_metadata_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.szls_engineeringMetadata_list,
      method: 'post',
      data: metadata_page_data.value,
    })
    .then((res) => {
      console.log(res)
      loading.value = false
      res.data.content.forEach((item) => {
        item.sort = parseInt(item.sort)
      })
      metadata_list.value = res.data.content
      metadata_total.value = parseInt(res.data.totalElements)
    })
    .catch((err) => {
      loading.value = false
      ElMessage.error(err)
    })
}
const submit_engineer = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      if (
        add_form_data.value.isCollection &&
        !add_form_data.value.engineeringIdList.length
      ) {
        ElMessage.error('请选择关联工程！')
        return
      }
      if (
        parseInt(add_form_data.value.pushFrequency) > 0 &&
        parseInt(add_form_data.value.pushFrequency) % 5 != 0
      ) {
        ElMessage.error('推送频率必须是5的倍数！')
        return
      }
      if (btn_loading.value) return
      btn_loading.value = true
      let data = JSON.parse(JSON.stringify(add_form_data.value))
      data.pic = !data.pic.length ? '' : data.pic.join()
      data.attachment = !data.attachment.length ? '' : data.attachment.join()
      axios
        .request({
          url:
            data.id == ''
              ? serviceApi.szls_engineering_crud
              : serviceApi.szls_engineering_crud + '/' + data.id,
          method: data.id == '' ? 'post' : 'put',
          data: data,
        })
        .then(() => {
          btn_loading.value = false
          ElMessage.success('操作成功！')
          setTimeout(() => {
            resetForm(formEl)
            get_list()
            show_box.value = false
          }, 1000)
        })
        .catch((err) => {
          ElMessage.error(err)
          btn_loading.value = false
        })
    }
  })
}
const delete_list = (e) => {
  ElMessageBox.confirm(
    e.row.status == 4 ? '确定还原：' + e.row.name : '确定删除：' + e.row.name,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal: false,
      type: 'error',
    }
  )
    .then(() => {
      axios
        .request({
          url:
            e.row.status != 4
              ? serviceApi.szls_engineering_crud + '/' + e.row.id
              : serviceApi.szls_engineering_restore + '/' + e.row.id,
          method: e.row.status != 4 ? 'delete' : 'put',
        })
        .then(() => {
          ElMessage.success('操作成功！')
          setTimeout(() => {
            get_list()
          }, 1000)
        })
        .catch((err) => {
          ElMessage.error(err)
        })
    })
    .catch(() => {})
}
const to_publish = (e) => {
  ElMessageBox.confirm(
    e.row.status == 1 ? '确定发布：' + e.row.name : '确定撤销：' + e.row.name,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal: false,
      type: 'warning',
    }
  )
    .then(() => {
      axios
        .request({
          url:
            e.row.status == 1
              ? serviceApi.szls_engineer_publish + '/' + e.row.id
              : serviceApi.szls_engineer_cancel + '/' + e.row.id,
          method: 'put',
        })
        .then(() => {
          ElMessage.success('操作成功！')
          setTimeout(() => {
            get_list()
          }, 1000)
        })
        .catch((err) => {
          ElMessage.error(err)
        })
    })
    .catch(() => {})
}
const refresh_list = (e) => {
  axios
    .request({
      url: serviceApi.szls_engineer_refresh + '/' + e.row.id,
      method: 'put',
    })
    .then(() => {
      ElMessage.success('刷新成功！')
    })
    .catch((err) => {
      ElMessage.error(err)
    })
}
const edit_list = (e) => {
  add_form_data.value = {
    projectId: add_form_data.value.projectId,
    engineeringIdList: e.row.engineeringIdList ? e.row.engineeringIdList : [],
    type: e.row.type ? e.row.type + '' : '1',
    isCollection: e.row.isCollection ? 1 : 0,
    pic: e.row.pic ? e.row.pic.split(',') : [],
    attachment: e.row.attachment ? e.row.attachment.split(',') : [],
    id: e.row.id,
    status: e.row.status ? e.row.status : '1',
    name: e.row.name,
    code: e.row.code,
    pushFrequency: e.row?.pushFrequency,
    publishTime: e.row.publishTime,
    publishByName: e.row.publishByName,
  }
  store.state.user_list.some((item) => {
    if (item.id == e.row.publishBy) {
      add_form_data.value.publishByName = item.name
    }
  })
  show_box.value = true
}
const btn_loading = ref(false)
const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.szls_engineering_list,
      method: 'post',
      data: page_data.value,
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      ElMessage.error(err)
      loading.value = false
    })
}
let list_debounce = debounce(200, get_list)
let metadata_list_debounce = debounce(200, get_metadata_list)
const search_key = () => {
  page_data.value.name = header_list.value[0].value
  page_data.value.code = header_list.value[1].value
  page_data.value.status = header_list.value[2].value as any
  get_list()
}
const resetForm = (formEl: FormInstance | undefined) => {
  console.log(123)
  if (!formEl) return
  formEl.resetFields()
  add_form_data.value = {
    id: '',
    name: '',
    code: '',
    type: '1',
    isCollection: 0,
    pic: [],
    status: '1',
    attachment: [],
    engineeringIdList: [],
    publishTime: '',
    publishByName: '',
    pushFrequency: '',
    projectId: add_form_data.value.projectId,
  }
  add_metadata_form_data.value = {
    id: '',
    name: '',
    code: '',
    sort: '',
    remark: '',
    status: true,
    type: '',
    value: '',
    paraList: [],
    isCombination: false,
    engineeringId: add_metadata_form_data.value.engineeringId,
    projectId: add_metadata_form_data.value.projectId,
  }
  add_metadata_box.value = false
  show_box.value = false
}
</script>

<style lang="scss" scoped>
.top_item {
  border: 1px solid #ccc;
  cursor: pointer;
  padding: 5px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  &:hover {
    color: #3580eb;
    border-color: #3580eb;
  }
}
.line {
  background-color: #333;
  width: 1px;
  height: 16px;
  margin: 0 10px;
}
</style>
